﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>OnNotify (GUI) - 语法 &amp; 使用 | AutoHotkey v2</title>
<meta name="description" content="The OnNotify method of a GuiControl object registers a function or method to be called when a control notification is received via the WM_NOTIFY message." />
<meta name="ahk:equiv-v1" content="commands/GuiControl.htm" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css">
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>OnNotify</h1>
<p>Registers a function or method to be called when a control notification is received via the <a href="https://msdn.microsoft.com/library/bb775583">WM_NOTIFY</a> message.</p>
<pre class="Syntax">GuiControl.<span class="func">OnNotify</span>(NotifyCode, Callback <span class="optional">, AddRemove := 1</span>)</pre>

<h2>参数</h2>
<dl>
  <dt>GuiControl</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#objects">对象</a></p>
    <p>The <a href="GuiControl.htm">GuiControl</a> object of the control to monitor.</p>
  </dd>
  
  <dt>NotifyCode</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#numbers">整数</a></p>
    <p>The control-defined notification code to monitor.</p>
  </dd>
  
  <dt>Callback</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a> or <a href="../Concepts.htm#objects">Object</a></p>
    <p>The function, method or object to call when the event is raised.</p>
    <p>If this parameter is a string, its meaning depends on whether the GUI has an <a href="../objects/Gui.htm#EventObj">event sink</a> (that is, whether <a href="../objects/Gui.htm#New">Gui.New</a>'s <em>EventObj</em> parameter was specified). If the GUI has an event sink, the string must be the name of a method belonging to the event sink; otherwise, it must be the name of a function.</p>
    <p>To register a function regardless of whether the GUI has an event sink, pass a <a href="Func.htm">function reference</a>.</p>
  </dd>
  
  <dt>AddRemove</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#numbers">整数</a></p>
    <p>One of the following values:<br>
    <strong>1</strong> (the default): Call the callback after any previously registered callbacks.<br>
    <strong>-1</strong>: Call the callback before any previously registered callbacks.<br>
    <strong>0</strong>: Do not call the callback.</p>
  </dd>
</dl>

<h2 id="WM_NOTIFY">WM_NOTIFY</h2>
<p>Certain types of controls send a <a href="https://msdn.microsoft.com/library/bb775583">WM_NOTIFY</a> message whenever an interesting event occurs or the control requires information from the program. The <em>lParam</em> parameter of this message contains a pointer to a structure containing information about the notification. The type of structure depends on the notification code and the type of control which raised the notification, but is always based on <a href="https://msdn.microsoft.com/library/bb775514">NMHDR</a>.</p>
<p>To determine which notifications are available (if any), what type of structure they provide and how they interpret the return value, refer to the control's documentation. <a href="https://msdn.microsoft.com/library/bb773169">Control Library (MSDN)</a> contains links to each of the the Windows common controls. The notification codes (numbers) can be found in the Windows SDK, or by searching the Internet.</p>
<p>AutoHotkey uses the <em>idFrom</em> and <em>hwndFrom</em> fields to identify which control sent the notification, in order to dispatch it to the appropriate object. The <em>code</em> field contains the notification code. Since these must match up to the <em>GuiControl</em> and <em>NotifyCode</em> used to register the callback, they are rarely useful to the script.</p>

<h2 id="Callback_Parameters">Callback Parameters</h2>
<p>The <a href="GuiOnEvent.htm#Callback_Parameters">notes for OnEvent</a> regarding <code>this</code> and bound functions also apply to OnNotify.</p>
<p>The callback receives two parameters:</p>
<pre class="Syntax"><i>Callback</i>(GuiControl, lParam)</pre>
<p>As noted above, <em>lParam</em> is the address of a notification structure derived from <a href="https://msdn.microsoft.com/library/bb775514">NMHDR</a>. Its exact type depends on the type of control and notification code. If the structure contains additional information about the notification, the callback can retrieve it with <a href="../commands/NumGet.htm">NumGet</a> and/or <a href="../commands/StrGet.htm">StrGet</a>.</p>

<h2 id="Callback_Return_Value">Callback Return Value</h2>
<p>If multiple callbacks have been registered for an event, a callback may return a non-empty value to prevent any remaining callbacks from being called.</p>
<p>The return value may have additional meaning, depending on the notification. 例如, the ListView notification <a href="https://msdn.microsoft.com/library/bb774798">LVN_BEGINLABELEDIT</a> (-175 or -105) prevents the user from editing the label if the callback returns TRUE (1).</p>

<h2 id="Related">Related</h2>
<p>These notes for <a href="GuiOnEvent.htm">OnEvent</a> also apply to OnNotify: <a href="GuiOnEvent.htm#Threads">Threads</a>, <a href="GuiOnEvent.htm#Destroying_the_GUI">Destroying the GUI</a>.</p>
<p><a href="GuiOnCommand.htm">OnCommand</a> can be used for notifications which are sent as a WM_COMMAND message.</p>

</body>
</html>